package com.kehutong.charge.admin;

import com.kehutong.charge.entity.ChargeOrderItem;
import com.kehutong.charge.service.ChargeOrderItemService;
import com.kehutong.charge.util.CommonUtil;
import com.kehutong.charge.util.enums.ChargeOrderStatusEnum;
import com.kehutong.common.DeptService;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;

import java.util.List;


/**
 * @program: kht
 * @description: 订单管理-子订单管理
 * @author: joneron
 * @create: 2021/7/5
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/order/item")
public class ChargeOrderItemController {

    private static final Logger logger = LoggerFactory.getLogger(ChargeOrderItemController.class);

    @Inject
    DeptService deptService;

    @Inject
    ChargeOrderItemService chargeOrderItemService;

    @ReqMapping("/page")
    @HasPermission("pay:charge:order:view")
    public Object page(JSONObject jsonObject) {
        return chargeOrderItemService.findPage(jsonObject);
    }

    @ReqMapping("/list")
    @HasPermission("pay:charge:order:view")
    public Object list(JSONObject jsonObject) {
        return chargeOrderItemService.findList(jsonObject);
    }

    @ReqMapping("/save")
    public Object save(JSONObject jsonObject, Token token) throws Exception {

        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0) {
            logger.error("用户{}数据异常，部门为空！", token.getUsername());

            DeptDTO deptDTO = deptService.getDept(departList.get(0));

            JSONArray jsonArray = jsonObject.getJSONArray("itemBillList");

            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject objs = (JSONObject) jsonArray.get(i);
                ChargeOrderItem item = Pool.newInstance(ChargeOrderItem.class);
                item.setOrderNo(objs.getString("orderNo"));
                item.setItemBillNo(objs.getString("itemBillNo"));
                item.setNegotiateAmount(objs.getBigDecimal("negotiateAmount"));
                item.setChargeName(objs.getString("chargeName"));
                item.setUserNo(objs.getString("userNo"));
                item.setPayableAmount(objs.getBigDecimal("payableAmount"));
                item.setCalcUnit(objs.getString("calcUnit"));
                item.setOrderAmount(objs.getBigDecimal("billAmount"));
                item.setHouseNo(objs.getString("houseNo"));
                item.setFeeTypeName(objs.getString("feeTypeName"));
                item.setFeeTypeNo(objs.getString("feeTypeNo"));

                //生成子订单号
                String orderNo = CommonUtil.generateRandom("DD");
                item.setOrderItemNo(orderNo);
                item.setCreateName(token.getUsername());
                item.setDepartmentNo(deptDTO.getDepartmentNo());
                item.setDepartmentNos(deptDTO.getDepartmentNos());
                item.setDepartmentName(deptDTO.getDepartmentName());

                chargeOrderItemService.save(item);
            }
        }

        return Result.success();
    }

    @Bean(copy=true)
    @HasPermission("pay:charge:order:edit")
    @ReqMapping("/update")
    public Object update(ChargeOrderItem entity) throws Exception {

        return chargeOrderItemService.update(entity);
    }

    @HasPermission("pay:charge:order:edit")
    @ReqMapping("/delete")
    public Object delete(ChargeOrderItem entity) {

        return chargeOrderItemService.deleteById(entity.getId());
    }

    @ReqMapping("/get")
    public Object get(ChargeOrderItem entity) {

        return Result.success(entity);
    }

    @ReqMapping("/getObj")
    public Object getObj(JSONObject jsonObject) {

        return Result.success(chargeOrderItemService.getObj(jsonObject));
    }

    /**
     * 退款查询接口
     */
    @ReqMapping("/refund/data")
    public Object refundData(JSONObject jsonObject) {

        //传入参数：itemBillNo 账单编号，userNo 用户编号

        jsonObject.put("orderStatus", ChargeOrderStatusEnum.ORDER_PAY_SUCCESS.getId());
        return chargeOrderItemService.findList(jsonObject);
    }

}
